package top.whysu.edevice.controller.manage;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.whysu.edevice.dto.device.DeviceAddModifyDTO;
import top.whysu.edevice.dto.device.DeviceQueryDTO;
import top.whysu.edevice.dto.generate.RequestDTO;
import top.whysu.edevice.dto.generate.ResponseDTO;
import top.whysu.edevice.dto.page.PagenationDataDTO;
import top.whysu.edevice.po.manage.ManageDevicePO;
import top.whysu.edevice.service.manage.ManageDeviceService;
import top.whysu.edevice.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 设备管理
 */
@Controller
@RequestMapping("/manageDevice")
public class ManageDeviceController {

    @Resource
    private ManageDeviceService manageDeviceService;

    @RequestMapping("/manageDevice")
    public String deviceManager() {
        return "manage/manageDevice";
    }

    /**
     * 新增、修改
     */
    @PostMapping("/addModifyDevice")
    @ResponseBody
    public ResponseDTO addModifyDevice(@RequestBody @Validated RequestDTO<DeviceAddModifyDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageDeviceService.wholeAddModifyDevice(requestDTO.getBody()));
    }

    /**
     * 设备列表--二维表格
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<ManageDevicePO>> getPage(@RequestBody DeviceQueryDTO deviceQueryDTO) {
        PagenationDataDTO<ManageDevicePO> page = manageDeviceService.selectPage(deviceQueryDTO);
        return ResponseUtil.getSuccessResponse(page);
    }

    /**
     * 删除设备
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseDTO delete(@RequestBody RequestDTO<BigDecimal> request) {
        manageDeviceService.delete(request.getBody());
        return ResponseUtil.getSuccessResponse();
    }

}